﻿//AcademicPlanner - course registration planning web app.
//Copyright (C) 2009  Boubacar Diallo

//This program is free software; you can redistribute it and/or
//modify it under the terms of the GNU General Public License
//as published by the Free Software Foundation; either version 2
//of the License, or (at your option) any later version.

//This program is distributed in the hope that it will be useful,
//but WITHOUT ANY WARRANTY; without even the implied warranty of
//MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
//GNU General Public License for more details.

//You should have received a copy of the GNU General Public License
//along with this program; if not, write to the Free Software
//Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.


using System;
using System.Collections.Generic;
using System.Web;
using System.Web.Mvc;

namespace AcademicPlanner.Framework.Libraries.Mvc
{
	/// <summary>
	/// Represents an Mvc Controller filter that ensures an action is executed
	/// only by ajax request.
	/// </summary>
	/// <since>1.0</since>
	[AttributeUsage(AttributeTargets.Method, AllowMultiple = false, Inherited = true)]
	public sealed class AjaxOnlyAttribute : FilterAttribute, IActionFilter
	{

		void IActionFilter.OnActionExecuted(ActionExecutedContext filterContext)
		{
			
		}

		/// <summary>
		/// Checks that the current web request is made through ajax.
		/// </summary>
		/// <param name="filterContext">The context of the filtering.</param>
		/// <exception cref="T:System.Web.HttpException">
		/// Thrown with status code 404 if the request is not an ajax one.
		/// </exception>
		/// <since>1.0</since>
		public void OnActionExecuting(ActionExecutingContext filterContext)
		{
			if (!filterContext.HttpContext.Request.IsAjaxRequest())
			{
				throw new HttpException(404, "The action requested is available through ajax only!");
			}
		}

	}
}
